//: Playground - noun: a place where people can play

//: # swfit 读书笔记

//: ## 控制流 (For-In 循环、While 循环、条件语句、控制转移语句（Control Transfer Statements）、提前退出、检测 API 可用性)


//: ### For-In 循环
//: 用来遍历集合中的所有元素
//: 普通 for 循环 如果不需要 index 变量 用 _ 代替
print("_____________________普通 for 循环_____________________________")
for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}
//: 数组普通遍历
print("_____________________ 数组普通遍历 _____________________________")
let forArr = ["擎天柱","大黄蜂","汽车人"]
for name in forArr {
    print(name)
}
//: 字典不同遍历
print("_____________________ 字典不同遍历 _____________________________")
let forDic = ["boss":"擎天柱","employee":"大黄蜂"];
for (key,value) in forDic {
    print("\(key)是\(value)")
}

//: ### While 循环 会一直循环到条件为 false
//: while 循环，在每次循环开始时计算是否满足条件
print("_____________________ while 循环 _____________________________")
var whileTempNum:Int = 0;
while whileTempNum < 3{
    whileTempNum += 1
    print("while - \(whileTempNum)")
}

//: repeat - while 在每次循环结束时计算是否满足条件
print("_____________________ repeat - while _____________________________")
var repeatWhileNum:Int = 0
repeat{
    repeatWhileNum += 1
    print("repeatWhileNum - \(repeatWhileNum)")
}while repeatWhileNum < 3
    print(" repeat - while 不满足条件了")



//: ### 条件语句 if 和 oc 没有啥区别


//: ### 控制转移语句  switch 尝试把某种条件和若干条件匹配
print("_____________________ switch _____________________________")
let someCharacter:Character = "z"
// 字符匹配
print("_____________________ 字符匹配 _____________________________")
switch someCharacter {
case "A":
    print("a")
case "z":
    print("z")
default:
    print("没有")
}

//: 区间匹配
let spaceNum:Int = 50
print("_____________________ 区间匹配 _____________________________")
switch spaceNum {
case 1..<5:
    print("1..<5")
case 5...6:
    print("5...6")
default:
    print("区间匹配")
}

//: 元组匹配
print("_____________________ 元组匹配 _____________________________")
let switchCouple = (12,12)

switch switchCouple {
case (11,11):
    print("(11,11)")
case (12,12):
    print("(12,12)")
default:
    print("元组匹配")
}
//: 值绑定 (特殊)
print("_____________________ 值绑定 (特殊) _____________________________")
let SwitchBind = (2,0)

switch SwitchBind {
case (let x,0):
    print("x 坐标是 \(x)，y 左边是 0")
case (0,let y):
    print("x 坐标是 0，y 左边是 \(y)")
case (let x,let y):
    print("x 坐标是 \(x)，y 左边是 \(y)")
}
//: 使用where语句来判断额外的条件 (特殊)
print("_____________________ 使用where语句来判断额外的条件 _____________________________")
let yetAnPoint = (1,-1)
switch yetAnPoint {
case let(x,y) where x == y :
    print("x y 相同")
case let(x,_) where x < 0 :
    print("x 小于 0")
case let(_,y) where y < 0 :
    print("y 小于 0")
case let(x,y) where x == -y:
    print("\(x),\(y) 在一条线上")
default:
    print("other")
}

//: ### 复合匹配  当多个条件可以使用同一种方法来处理时，可以将这几种可能放在同一个case后面，并且用逗号隔开。当case后面的任意一种模式匹配的时候，这条分支就会被匹配。并且，如果匹配列表过长，还可以分行书写：
print("_____________________ 复合匹配 _____________________________")
let somedCharacter: Character = "e"
switch somedCharacter {
case "a", "e", "i", "o", "u":
    print("\(somedCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
     "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
    print("\(somedCharacter) is a consonant")
default:
    print("\(somedCharacter) is not a vowel or a consonant")
}

let stillAnotherPoint = (9, 0)
switch stillAnotherPoint {
case (let distance, 0), (0, let distance):
    print("On an axis, \(distance) from the origin")
default:
    print("Not on an axis")
}





